package com.smr.micservice.apigateway.config.bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * swagger配置文件
 *
 * @author shi_mengran
 */
@Slf4j
@Configuration
@EnableSwagger2
public class SwaggerConfigBean {

    private static String HEADER = "Authentication";

    @Bean
    public Docket customDocket() {
        //添加全局响应类型
        ResponseMessageBuilder responseMessageBuilder = new ResponseMessageBuilder();
        List<ResponseMessage> responseMessages = Arrays.asList(
                responseMessageBuilder.code(HttpStatus.BAD_REQUEST.value()).message("参数校验异常,如果当前提交的参数未通过校验，则抛出此状态码").build(),
                responseMessageBuilder.code(HttpStatus.UNAUTHORIZED.value()).message("token认证失败,通常是token过期或者token有误").build(),
                responseMessageBuilder.code(HttpStatus.FORBIDDEN.value()).message("访问被拒绝,没有权限访问该接口").build()
        );

        return new Docket(DocumentationType.SWAGGER_12)
                .select()
                .build()
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.POST, responseMessages)
                .apiInfo(apiInfoTest());
    }

    /**
     * 测试线api信息
     *
     * @return api信息
     */
    private ApiInfo apiInfoTest() {
        return new ApiInfoBuilder()
                .title("jshq-social-webapi")
                .description("api组合调用:" +
                        "使用说明: " +
                        "所有接口都要通过验证,通过暴露的/login地址发送请求, " +
                        "请求的payload格式 : {\"username\":\"lisi\",\"password\":\"456789\"}。" +
                        "请求成功后,会返回包装后的数据{\"status\":200,\"message\":\"\",\"data\":\"token\"}," +
                        "其中data中包含的数据为token, " +
                        "因为token有效期为2天,建议每天刷新1~2次token保证token持续有效,")
                .license("")
                .licenseUrl("")
                .contact(new Contact("shimengran", "", "smr@qinqinxiaobao.com"))
                .termsOfServiceUrl("https://www.qinqinxiaobao.com")
                .version("1.0.0")
                .build();
    }
}
